/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import pojo.BinhLuanPoJo;
import util.DataProvider;

/**
 *
 * @author Kanet
 */
public class BinhLuanDao {
    // <editor-fold defaultstate="collapsed" desc="1. Chung">
     private static ArrayList<BinhLuanPoJo> LayDanhSach(String strSql){
        ArrayList<BinhLuanPoJo> danhSach=null;
        DataProvider dp=new DataProvider();
        dp.open();
        try {
            ResultSet rs = dp.executeQuery(strSql);
            danhSach=new ArrayList<BinhLuanPoJo>();
            BinhLuanPoJo sp=null;
            while (rs.next()) {
                sp=new BinhLuanPoJo();
                sp.setIdBinhLuan(rs.getInt("idBinhLuan"));
                sp.setIdBanTin(rs.getInt("idBanTin"));
                sp.setNoiDung(rs.getString("noiDung"));
                sp.getBinhLuanCha().getBinhLuanCha().setIdBinhLuan(rs.getInt("idBanTin"));            
                
                danhSach.add(sp);
            }
            dp.close();
        } catch (SQLException ex) {
            Logger.getLogger(BinhLuanDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return danhSach;
    }
    private static int LayDong(String strSql){
        int soDong=0;
        DataProvider dp=new DataProvider();
        dp.open();
        try{
            ResultSet rs = dp.executeQuery(strSql);
            rs.getFetchSize();
            while(rs.next())
                soDong++;
            dp.close();
        } catch (SQLException ex) {
            Logger.getLogger(BinhLuanDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return soDong;
    }
// </editor-fold>
   
    public static ArrayList<BinhLuanPoJo> DanhSachBinhLuan_PhanTrang(int sotrang, int sobantin)
    {
        int batdau =(sotrang-1)*sobantin;
        String sql ="Select * from binhluan";
        return LayDanhSach(sql);        
    }
    public static void ThemBinhLuan(BinhLuanPoJo bl) throws SQLException
    {
     DataProvider dp=new DataProvider();
        dp.open();
        try
        {
            String sql="insert into binhluan(noiDung,idBantin) values (?,?)";
            PreparedStatement pst=(PreparedStatement) dp.getConnection().prepareStatement(sql);            
            pst.setString(1, bl.getNoiDung()); 
            pst.setInt(2, bl.getIdBanTin());
            pst.execute();         
        }
        catch(Exception ex){}
        dp.close();   
    }
    public static ArrayList<BinhLuanPoJo> LayDanhSachTheoBanTin(int idBanTin){
        String sql="Select * from BinhLuan where idBanTin="+idBanTin;
        return BinhLuanDao.LayDanhSach(sql);
    }
    public static int LaySoDongTheoBanTin(int idBanTin){
        String sql="Select * from BinhLuan where idBanTin="+idBanTin;
        return BinhLuanDao.LayDong(sql);
    }
    
}
